﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kovan.Model.Parameter.UserCenter;
using Kovan.BLL.UserCenter;
using Kovan.Model.Entity.UserCenter;
using Kovan.BLL.Common;
using Kovan.Model.Enums;


namespace Kovan.UI.Areas.Manage.Controllers
{
    public class UserController : ManageControllerBase
    {

        public ActionResult Index(PUser param)
        {
            if (!(param.PageSize > 0))
            {
                param.PageSize = 20;
            }
            if (!(param.PageIndex > 0))
            {
                param.PageIndex = 1;
            }
            var items = BUserManage.GetInstances().GetUserItems(param);
            var dept = BActDept.GetInstances().GetList(WebConfig.CurrentUser.ApplicationID.Value);
            ViewBag.Depts = dept;
            ViewBag.Param = param;

            return View("index", items);
        }

        public ActionResult GetOperate(long? user_id)
        {
            act_user model = new act_user();
            if (user_id.HasValue)
            {
                model = BActUser.GetInstances().GetModel(user_id.Value);
                ViewBag.UserRoles = BActUserRole.GetInstances().GetList(model.user_id);
            }
            var dept = BActDept.GetInstances().GetList(WebConfig.CurrentUser.ApplicationID.Value);
            var role = BActRole.GetInstances().GetList(WebConfig.CurrentUser.ApplicationID.Value);
            ViewBag.Depts = dept;
            ViewBag.Roles = role;
            return View("OperateDialog", model);
        }

        public ContentResult Delete(string ids)
        {
            var statuscode = string.Empty;
            List<string> idss = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var result = BActUser.GetInstances().BatchDelete(idss);
            if (result > 0)
            {
                statuscode = GetSucceedCode("保存成功", "user_manage", Url.Content("~/Manage/User"), "用户管理");
            }
            return Content(statuscode);
        }

        public ActionResult SaveUser(act_user model, int? role)
        {
            int result = 0;
            string statuscode = string.Empty;
            if (model.user_id.HasValue)
            {
                var model1 = BActUser.GetInstances().GetModel(model.user_id.Value);
                model1.enabled = model.enabled ?? 0;
                model1.dept_id = model.dept_id;
                model1.email = model.email;
                model1.identity_card = model.identity_card;
                model1.last_modified_date = DateTime.Now;
                model1.last_modified_user = 0;//最后修改的用户
                model1.name = model.name;
                //密码 如果填写密码则修改密码
                model1.password = string.IsNullOrEmpty(model.password) ? model1.password : model.password;
                model1.phone_1 = model.phone_1;
                model1.title = model.title;

                //角色修改
                if (role.HasValue)
                {
                    BActUserRole.GetInstances().DeleteByUserID(model.user_id.Value);
                    BActUserRole.GetInstances().Add(new act_user_role { role_id = role, user_id = model1.user_id });
                }

                result = BActUser.GetInstances().Update(model1);
            }
            else
            {
                if (string.IsNullOrEmpty(model.password))
                {
                    return Content(GetFailCode("操作失败，请填写密码", "", "", ""));
                }
                model.application_id = WebConfig.CurrentUser.ApplicationID.Value;
                model.create_date = DateTime.Now;
                model.ukey = Guid.NewGuid().ToString("N");
                result = BActUser.GetInstances().Add(model);
                if (result > 0)
                {
                    BActUserRole.GetInstances().Add(new act_user_role { role_id = role, user_id = model.user_id });
                }
            }

            if (result > 0)
            {
                //成功
                statuscode = GetSucceedCode("保存成功", "user_manage", Url.Content("~/Manage/User"), "用户管理");
            }
            else
            {
                //失败
                statuscode = GetFailCode("保存失败", "", "", "");
            }
            return Content(statuscode);
        }

        public ActionResult OperatePwd()
        {
            return View("OperatePwd");
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="opwd"></param>
        /// <param name="pwd1"></param>
        /// <param name="pwd2"></param>
        /// <returns></returns>
        public ContentResult SavePwd(string opwd, string pwd1, string pwd2)
        {
            string statuscode = string.Empty;
            if (string.IsNullOrEmpty(opwd) || string.IsNullOrEmpty(pwd1) || string.IsNullOrEmpty(pwd2))
            {
                //失败
                statuscode = GetFailCode("保存失败,请填写密码", "", "", "");
            }
            else if (pwd1 != pwd2)
            {
                //失败
                statuscode = GetFailCode("保存失败,密码不一致", "", "", "");
            }
            else
            {
                var ucModel = BActUser.GetInstances().GetModel(WebConfig.CurrentUser.UserID.Value);
                if(ucModel==null || ucModel.password!=opwd)
                {
                    //失败
                    statuscode = GetFailCode("保存失败,密码不正确", "", "", "");
                }
                ucModel.password = pwd1;
                BActUser.GetInstances().Update(ucModel);
                statuscode = GetResultCode(new Model.Common.ResultCode<string> { callbackType = "closeCurrent", statusCode = ((int)StatusEnum.Succeed).ToString(), message="操作成功" });
            }
            return Content(statuscode);
        }
    }
}
